package com.young.dubbo.protocol.dubbo;

import com.young.dubbo.framework.Invocation;
import com.young.dubbo.provider.LocalRegister;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.lang.reflect.Method;

/**
 * 继承ChannelInboundHandlerAdapter，用于处理客户端发送过来的请求
 */
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Invocation invocation = (Invocation) msg;
        Class serviceImpl = LocalRegister.get(invocation.getInterfaceName());
        Method method = serviceImpl.getMethod(invocation.getMethodName(), invocation.getParamTypes());
        Object result = method.invoke(serviceImpl.newInstance(), invocation.getParams());
        System.out.println("Netty------" + result.toString());
        ctx.writeAndFlush("Netty: " + result);
    }
}
